package algebra.rewriting.transform;

import entities.concept.AndConcept;
import entities.concept.Concept;
import algebra.op.AlgebraicOP;
import algebra.op.SelectionOP;
import algebra.rewriting.RewriteRule;

/**
 * When decompose selection conditions, the order of conjuncts is not
 * considered.
 * 
 * @author j55wu
 * 
 */
public class DecompSelection extends RewriteRule {
	private final AlgebraicOP op;

	public DecompSelection(AlgebraicOP op) {
		this.op = op;
	}

	@Override
	public AlgebraicOP rewrite() {
		AndConcept conds = (AndConcept) op.asSelectionOP()
				.getSelectionCondition();
		AlgebraicOP q = op.asSelectionOP().getSubquery();
		for (Concept c : conds.getConceptSet()) {
			q = new SelectionOP(q, c, op.asSelectionOP().getPlanEvalKB());
		}
		return q;
	}

	@Override
	public boolean applicable() {
		if (op instanceof SelectionOP
				&& (op.asSelectionOP().getSelectionCondition() instanceof AndConcept)) {
			return true;
		}
		return false;
	}

}
